package org.docshare.extract;


import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;


public class Page {
	public static String stub = "";// "http://www.sdutj.cn/medicine/stub.asp?u=";
	public static String charset = "utf-8";
	public static void setCharset(String str)
	{
		charset = str;
	}
	public static String getPage(String url)
	{
		try{
			URLConnection c = new URL(url).openConnection();
			c.setReadTimeout(30*1000);
			//c.setRequestProperty("User-Agent", "soduspider"); 
			c.setRequestProperty("Referer", url);
	        c.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"); 
	
			System.out.println(c.getContentType());
	        int l = c.getContentLength();
	        byte[] data = new byte[l];
	        int d = c.getInputStream().read(data);
	        
	        String str = new String(data,"utf-8");
	        if(str.contains("gbk")|| str.contains("utf-8")){
	        	str = new String(data,"gbk");
	        }
			return str;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
		
	}
	public static String getPage(String url_str,String my_charset,int use_sec)
	{
	    	url_str = url_str.replace("http://www.kanshu.la/", "http://www.kanshu.la.sixxs.org/");
		long start = System.currentTimeMillis();
		String r = getPage(url_str,my_charset);
		if(System.currentTimeMillis() - start < use_sec*1000)
		{
			try {
				Thread.sleep(use_sec*1000 + start - System.currentTimeMillis());
			} catch (InterruptedException e) {}
		}
		return r;
	}
	public static int retry_time = 4;
	public static String getPage(String url_str,String my_charset)
	{
		URL url=null;
		//get url 
		try {
			url = new URL(stub + url_str);
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return null;
		}

		String s=null;
		//String charset = "utf-8";
		for(int i=0;i<retry_time;i++)
		{
			try{
				URLConnection c = url.openConnection();
				c.setReadTimeout(30*1000);
				//c.setRequestProperty("User-Agent", "soduspider"); 
				c.setRequestProperty("Referer", url_str);
	            c.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"); 

				System.out.println(c.getContentType());
	            BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream(),my_charset));  
				StringBuffer sb = new StringBuffer("");
			
				while ((s = br.readLine()) != null) {
					sb.append(s + "\r\n"); 
				}
				br.close();
				s = sb.toString();
				if(s != "$False$" && s.length() > 30) break;
			}catch(SocketTimeoutException e)
			{
				if(i == retry_time-1){
					System.out.println("Get Page Fail!");
					//e.printStackTrace();
				}else{
					System.out.println("read time out !" + i + " times");	
					//e.printStackTrace();
				}
				s = null;
				try{
					Thread.sleep(50000);
				}catch(Exception ee){}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				break;
			}
		}

		return s;
	}
	public static String getPageNew(String url_str,String my_charset)
	{
	    int retry_time = 1;
		URL url=null;
		//get url 
		try {
			url = new URL(stub + url_str);
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return null;
		}

		String s=null;
		//String charset = "utf-8";
		for(int i=0;i<retry_time;i++)
		{
			try{
				URLConnection c = url.openConnection();
				c.setReadTimeout(30*1000);
				c.setConnectTimeout(20*1000);
				//c.setRequestProperty("User-Agent", "dcspider"); 
				c.setRequestProperty("Referer", url_str);
				
	            c.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"); 
				BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream(),my_charset));  
				StringBuffer sb = new StringBuffer("");
			
				while ((s = br.readLine()) != null) {
					sb.append(s + "\r\n"); 
				}
				br.close();
				s = sb.toString();
				if(s != "$False$" && s.length() > 10) break;
			}catch(SocketTimeoutException e)
			{
				if(i == retry_time-1){
					System.out.println("Get Page Fail!"+url_str);
					//e.printStackTrace();
				}else{
					System.out.println("read time out !" + i + " times");	
					//e.printStackTrace();
				}
				s = null;
				//try{
				//	Thread.sleep(50);
				//}catch(Exception ee){}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				break;
			}
		}

		return s;
	}


	public static String getPageBefore(String url_str,String my_charset,String mask)
	{
	    int retry_time = 1;
		URL url=null;
		//get url 
		try {
			url = new URL(stub + url_str);
		} catch (MalformedURLException e) {
			e.printStackTrace();
			return null;
		}

		String s=null;
		//String charset = "utf-8";
		for(int i=0;i<retry_time;i++)
		{
			try{
				URLConnection c = url.openConnection();
				c.setReadTimeout(20*1000);
				c.setRequestProperty("User-Agent", "dcspider"); 
				c.setRequestProperty("Referer", url_str);
				
	            //c.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"); 
				BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream(),my_charset));  
				StringBuffer sb = new StringBuffer("");
			
				while ((s = br.readLine()) != null) {
					sb.append(s + "\r\n"); 
					if(s.contains(mask))break;
				}
				br.close();
				s = sb.toString();
				if(s != "$False$" && s.length() > 10) break;
			}catch(SocketTimeoutException e)
			{
				if(i == retry_time-1){
					System.out.println("Get Page Fail!");
					//e.printStackTrace();
				}else{
					System.out.println("read time out !" + i + " times");	
					//e.printStackTrace();
				}
				s = null;
				try{
					Thread.sleep(50);
				}catch(Exception ee){}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				break;
			}
		}

		return s;
	}
	
	
	public static void main(String[] args)
	{
		Page.setCharset("gb2312");
		String page=Page.getPageNew("http://www.79sy.com/top/lastupdate-1.html","GBK");
		System.out.println(page);
		System.out.println("helo");
	}
	
	public static boolean downBinFile(String url,String savefile,String ref) 
	{
		
		try{
			//url = URLEncoder.encode(url,"UTF-8");
			URL u = new URL(url);
			URLConnection uc = u.openConnection();
			uc.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 5.0; Windows XP; DigExt)");
			uc.setRequestProperty("referer", ref);
/*
   		String contentType = uc.getContentType();
*/
 
		int contentLength = uc.getContentLength();
/*
		if (contentType != null && (contentType.startsWith("text/") || (contentLength == -1)))
		{
			throw new IOException("this is not a binary file.");
		}
*/
		
		InputStream raw = uc.getInputStream();
		InputStream in = new BufferedInputStream(raw);
		byte[] data = new byte[contentLength];
		int bytesRead = 0;
		int offset = 0;
		while (offset < contentLength)
		{
			bytesRead = in.read(data,offset,data.length-offset);
			if(bytesRead == -1) break;
			offset +=bytesRead;
		}
		in.close();

		if (offset != contentLength)
		{
			throw new IOException("Only read "+offset+" bytes;Expected "+contentLength+" bytes.");
		}
		
		String fileName = u.getFile();
		@SuppressWarnings("unused")
		String type = fileName.substring(fileName.lastIndexOf("."));
		//String filePath = "D:\\java\\";
		//fileName = fileName.substring(fileName.lastIndexOf('/')+1);
		//if(savefile.indexOf(".") < 0) savefile =savefile+type;
		
		
		FileOutputStream fout = new FileOutputStream(savefile);
		fout.write(data);
		fout.flush();
		fout.close();		
		}catch(Exception e)
		{
			System.out.println("Down Fail:" + url);
			e.printStackTrace();
			return false;
		}
		
		return true;
	}
	

}
